package com.data.center.mapper;

import cn.hutool.core.bean.BeanUtil;
import com.data.center.dto.PersonDto;
import com.data.center.entity.Person;
import com.data.center.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Slf4j
class PersonMapperTestSC {

    @Resource
    private PersonMapper personMapper;

    @Autowired
    private PersonService personService;

    @Test
    public void testGetPersonById() {
        // Test data
        String personId = "P-12345";
        Person expectedPerson = new Person();
        expectedPerson.setPersonId(personId);

        // Mocking the behavior of the personMapper.getPersonById method
        Mockito.when(personMapper.getPersonById(personId)).thenReturn(expectedPerson);

        // Calling the corresponding method in the service
        Person actualPerson = personService.getPersonById(personId);

        // Add assertions based on your specific needs
        // Example:
         assertEquals(expectedPerson, actualPerson);
    }

    // Test for getAllPersons method
    @Test
    public void testGetAllPersons() {
        // Test data
        List<Person> expectedPersons = new ArrayList<>();
        // Add some dummy data to the list

        // Mocking the behavior of the personMapper.getAllPersons method
        Mockito.when(personMapper.getAllPersons()).thenReturn(expectedPersons);

        // Calling the corresponding method in the service
        List<Person> actualPersons = personService.getAllPersons();

        // Add assertions based on your specific needs
        // Example:
         assertEquals(expectedPersons.size(), actualPersons.size());
    }

    // Test for addPerson method
    @Test
    public void testAddPerson() {
        // Test data
        Person personToAdd = new Person();
        personToAdd.setPersonId("P-12345");
        personToAdd.setName("Test");
        // Set properties for the person to add

        // Calling the corresponding method in the service
        personService.addPerson(BeanUtil.copyProperties(personToAdd, PersonDto.class));

        // Add assertions or verify mocks based on your specific needs
        // Example:
//         Mockito.verify(personMapper).insertPerson(personToAdd);
    }

    // Test for updatePerson method
    @Test
    public void testUpdatePerson() {
        // Test data
        Person personToUpdate = new Person();
        // Set properties for the person to update

        // Calling the corresponding method in the service
        personService.updatePerson(personToUpdate);

        // Add assertions or verify mocks based on your specific needs
        // Example:
         Mockito.verify(personMapper).updatePerson(personToUpdate);
    }

    // Test for deletePerson method
    @Test
    public void testDeletePerson() {
        // Test data
        String personIdToDelete = "P-12345";

        // Calling the corresponding method in the service
        personService.deletePerson(personIdToDelete);

        // Add assertions or verify mocks based on your specific needs
        // Example:
         Mockito.verify(personMapper).deletePerson(personIdToDelete);
    }

    @Test
    public void testFindByAnyKey() {
        // 构造查询参数
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("personId", "P-12345");

        // 查询
        List<Person> result = personMapper.selectByMap(queryParams);

        // 断言
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals("P-12345", result.get(0).getPersonId());
    }

    @Test
    public void testUpdateByAnyKey() {
        // 构造更新参数
//        Map<String, Object> updateParams = new HashMap<>();
//        updateParams.put("person_id", "P-12345");
//        updateParams.put("name", "Updated Name");
        Person person = new Person();
        person.setPersonId("P-12345");
        person.setName("Updated Name");
        // 更新
        personMapper.updateById(person);

        // 查询更新后的记录
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("person_id", "P-12345");
        List<Person> updatedResult = personMapper.selectByMap(queryParams);

        // 断言
        assertNotNull(updatedResult);
        assertEquals(1, updatedResult.size());
        assertEquals("Updated Name", updatedResult.get(0).getName());
    }

    @Test
    public void testDeleteByAnyKey() {
        // 构造删除参数
        Map<String, Object> deleteParams = new HashMap<>();
        deleteParams.put("person_id", "P-12345");

        // 删除
        personMapper.selectByMap(deleteParams);

        // 查询删除后的记录
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("person_id", "P-12345");
        List<Person> deletedResult = personMapper.selectByMap(queryParams);

        // 断言
        assertNotNull(deletedResult);
        assertEquals(0, deletedResult.size());
    }

}